﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Aulac.Global.Enums; using Aulac.Global.Data; using Aulac.Global.Common; using Aulac.Global.Xml;
using Aulac.Rsc.Ems; using Aulac.Rsc.Ems.Tags;
using System.Xml.Linq;
using System.Data;
using Aulac.Srv.Db.Provider; using Aulac.Srv.Db.Connect;
using Aulac.Srv.Db.Data.Ems.InvJrns;
using Aulac.Rsc;

namespace Aulac.Srv.Db.Data.Ems.Stores
{
    /// <summary>
    /// Cap nhat hang ton kho, ghi tai khoan gia von
    /// </summary>
    public class InvJrnProvider: DbAulacDataManager
    {
        public InvJrnProvider(IDbAulacConnect pDb): base(pDb)
        {

        }

        /// <summary>
        /// Tao XmlInvJrn 
        /// </summary>
        /// <param name="xmlDoc"></param>
        /// <returns></returns>
        public XmlDataDocument CreateXmlInvJrn(XmlDataDocument xmlDoc)
        {
            XmlDataDocument xmlInStock = CreateXmlDocument("", "");
            XmlElement eV = XmlProcess.AddVchElm(ref xmlInStock);
            XmlElement eInvJrnHeader = XmlInvJrnHelper.AddInvJrnHeaderOfVch(eV);
            
            XmlElement eHeader = XmlProcess.GetVchHeaderElm(xmlDoc);
            XmlNodeList listItem = XmlProcess.GetListVchLineItem(xmlDoc);
            XmlProcess.SetAtrbFromElm(ref eInvJrnHeader, eHeader,
      tagGeneral.VchOrgID, tagGeneral.VchTypeID, tagGeneral.VchDate, tagGeneral.VchNo, tagGeneral.VchName,
      tagVch.Inv.Note);

            IEnumerable<XmlElement> l = listItem.Cast<XmlElement>();

            var result = from s in l.AsEnumerable()
                         group s by new
                         {
                             StorePrkID = s.GetAttribute(tagVch.Inv.StorePrkID),
                             ItemPrkID = s.GetAttribute(tagVch.Inv.ItemPrkID),
                             TagStore = (int)ValueHelper.ChangeType( s.GetAttribute(tagVch.Inv.TagStore), typeof(int))
                         } into grpData
                         select new
                             {
                                 grpData.Key,
                                 Quantity = (grpData.Key.TagStore == (int)TagStores.OutputStore)? -grpData.Sum(s => XmlProcess.GetValueDoubleAtrb(s, tagVch.Inv.Quantity)) : grpData.Sum(s => XmlProcess.GetValueDoubleAtrb(s, tagVch.Inv.Quantity))
                             };

            XmlElement eJrnLines = XmlInvJrnHelper.AddInvJrnLinesOfVch(eV);

            foreach (var e in result)
            {
                XmlElement eLine = XmlInvJrnHelper.AddInvJrnItem(eJrnLines);
                XmlProcess.SetAtrb(ref eLine, tagVch.Inv.StorePrkID, e.Key.StorePrkID);
                XmlProcess.SetAtrb(ref eLine, tagVch.Inv.ItemPrkID, e.Key.ItemPrkID);
                XmlProcess.SetAtrb(ref eLine, tagVch.Inv.TagStore, e.Key.TagStore);
                XmlProcess.SetAtrb(ref eLine, tagVch.Inv.Quantity, e.Quantity);
            }
                             
            /*DataTable lTb = null;
            Aulac.Global.Data.DataTableProcess.AddDataToTable(listItem, CultureInfo, ref lTb, true, false);

            var result = from s in lTb.AsEnumerable()
                         group s by new
                         {

                             StorePrkID = s.Field<string>(tagVch.Inv.StorePrkID),
                             ItemPrkID = s.Field<string>(tagVch.Inv.ItemPrkID),
                             TagStore = s.Field<int>(tagVch.Inv.TagStore)
                         } into grpTbl
                         select new
                             {
                                 grpTbl.Key,
                                 Quantity = grpTbl.Sum(s => s.Field<double>(tagVch.Inv.Quantity))
                             };
            
            */
  
            return xmlInStock;
        }
    }
   
}
